package TankGame03;

import java.io.*;
import java.util.Vector;

/**
 * @author 王艺锡
 * @version 1.0
 * 用于记录信息
 */

public class Recorder {
    private static int deadTankCount = 0;
    private static BufferedWriter bw = null;
    private static BufferedReader br = null;
    private static String filePath = "src\\MyRecord.txt";
    private static Vector<EnemyTank> enemyTanks = null;
    private static Vector<Node> nodes = new Vector<Node>();

    public static void setEnemyTanks(Vector<EnemyTank> enemyTanks) {
        Recorder.enemyTanks = enemyTanks;
    }

    public static String getFilePath() {
        return filePath;
    }

    public static int getDeadTankCount() {
        return deadTankCount;
    }

    public static void setDeadTankCount(int deadTankCount) {
        Recorder.deadTankCount = deadTankCount;
    }

    //当击毁地方坦克时，deadTankCount+1
    public static void addDeadTankCount() {
        Recorder.deadTankCount++;
    }

    //保存击败数据和敌方剩余坦克信息到文件
    public static void keepRecord() throws IOException {
        bw = new BufferedWriter(new FileWriter(filePath));
        bw.write(deadTankCount + "\r\n");

        //遍历集合，保存敌方坦克信息
        for (int i = 0; i < enemyTanks.size(); i++) {
            EnemyTank enemyTank = enemyTanks.get(i);
            if(enemyTank.isLive){
                bw.write(enemyTank.getX() + " " + enemyTank.getY() + " " + enemyTank.getDirect() + "\r\n");
            }
        }
        bw.close();
    }

    //编写方法，用于读取保存的数据
    public static Vector<Node> readContinueInfo(){

        try {
            br = new BufferedReader(new FileReader(filePath));
            //读取上局的击毁敌方坦克信息
            deadTankCount =Integer.parseInt(br.readLine());
            //读取上局的坦克坐标位置,放入集合nodes
            String line = "";
            while ((line = br.readLine()) != null){
                String[] s = line.split(" ");
                Node node = new Node(Integer.parseInt(s[0]),
                        Integer.parseInt(s[1]), Integer.parseInt(s[2]));
                nodes.add(node);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return nodes;
    }

}
